﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Weapon : MonoBehaviour {

	public enum WeaponType{Semi, Auto, Shotgun, Laser};
	public WeaponType currentType = WeaponType.Semi;
	public GameObject pistolBullet;
	Inventory inventory;
	float flashTimer = 0;
	float fireRateTimer = 0;
	public float fireRate = 60;
	public float accuracy = .5f;
	public float damage = 20;
	public int clipSize = 0;
	public GameObject laserHit;
	public float bulletsLeft;
	public Text ammoText;
	public Text reloadText;
	public bool reloading;
	public float reloadTime;
	public new bool enabled;
	float reloadTimer;
	LineRenderer line;
	GameObject barrel;
	public Sprite pistolHold;
	public Sprite rifleHold;
	public Sprite nothingHold;
	float laserRotOffset;
	void Start () {
		laserHit = GameObject.Instantiate(laserHit);
		laserHit.SetActive(false);
		barrel = transform.Find ("barrel").gameObject;
		line = barrel.GetComponent<LineRenderer> ();
		inventory = GameObject.FindGameObjectWithTag("StateManager").GetComponent<Inventory>();
		UpdateCharacter();
	}
	void Update() {
		AmmoUI ();
		enabled = (MiniGameManager.getInstance ().inProgress) ? false : enabled;
		if(!MiniGameManager.getInstance().inProgress && !enabled && !inventory.holder.gameObject.activeSelf) enabled = true;

		if (!reloading && Input.GetKey (KeyCode.R) && bulletsLeft != clipSize) {
			reloading = true;
			reloadTimer = reloadTime;
		}
		reloadTimer -= Time.deltaTime;
		if (reloading && reloadTimer <= 0) {
			reloading = false;
			bulletsLeft = clipSize;
		}
		if (enabled) {
			barrel.GetComponent<Light> ().intensity = flashTimer * 10;
			if (flashTimer > 0)
				flashTimer -= Time.deltaTime;
			fireRateTimer -= Time.deltaTime;
			if (!reloading && clipSize != 0)
				switch (currentType) {
				case WeaponType.Semi:
					line.enabled = false;
					if (Input.GetMouseButtonDown (0) && fireRateTimer <= 0 && bulletsLeft > 0) {
						Shoot (1000, .5f, damage, accuracy);
						fireRateTimer = (60 / fireRate);
						bulletsLeft--;
					}
					break;
				case WeaponType.Auto:
					line.enabled = false;
					if (Input.GetMouseButton (0) && fireRateTimer <= 0 && bulletsLeft > 0) {
						Shoot (1500, .3f, damage, accuracy);
						fireRateTimer = (60 / fireRate);
						bulletsLeft--;
					}
					break;
				case WeaponType.Shotgun:
					line.enabled = false;
					if (Input.GetMouseButtonDown (0) && fireRateTimer <= 0 && bulletsLeft > 0) {
						for (int i = 0; i < 6; i ++) 
							Shoot (800, .3f, damage, accuracy);
						fireRateTimer = (60 / fireRate);
						bulletsLeft--;
					}
					break;
				case WeaponType.Laser:
					line.enabled = false;
					if (Input.GetMouseButton (0) && fireRateTimer <= 0 && bulletsLeft > 0) {
						laserHit.SetActive (true);
						ShootLaser (damage * (fireRate / 60f), accuracy);
						bulletsLeft -= Time.deltaTime * (fireRate/120f);
					}
					if (Input.GetMouseButtonUp (0) || bulletsLeft <= 0) {
						laserRotOffset = 0;
						laserHit.SetActive (false);
					}
					break;
				}
		}
	}
	void Shoot(float velocity, float givenflashTimer, float damage, float givenAccuracy) {
		if (givenAccuracy < 25)
			givenAccuracy = 25;
		givenAccuracy = Mathf.Pow (givenAccuracy/5, 3f);
		GameObject tempBullet = (GameObject)GameObject.Instantiate(pistolBullet);
		Vector3 tempRot = transform.localRotation.eulerAngles;
		tempRot.y += (5000 * Random.Range(-1/(givenAccuracy), 1/(givenAccuracy)));
		tempBullet.GetComponent<Bullet>().velocity = velocity;
		tempBullet.GetComponent<Bullet>().damage = damage;
		tempBullet.transform.position = barrel.transform.position;
		tempBullet.transform.rotation = Quaternion.Euler(tempRot);
		flashTimer = givenflashTimer;
	}
	void ShootLaser(float dps, float givenAccuracy) {
		line.enabled = true;
		givenAccuracy = Mathf.Pow (givenAccuracy/5, 3f);
		line.SetPosition (0, barrel.transform.position);
		laserRotOffset = Mathf.Lerp(laserRotOffset, (90000 * Random.Range(-1/givenAccuracy, 1/givenAccuracy)), Time.deltaTime/20);
		RaycastHit hit;
		if(Physics.Raycast(barrel.transform.position, Quaternion.Euler(0, laserRotOffset, 0) * -transform.up, out hit)) {
			if(!hit.collider.isTrigger && hit.collider.tag != "Player") {
				line.SetPosition(1, hit.point);
				laserHit.transform.position = hit.point;
				line.material.mainTextureScale = new Vector2(hit.distance, 1);
			}
			else {
				line.SetPosition(1, barrel.transform.position + Quaternion.Euler(0, laserRotOffset, 0) * -transform.up * 100);
				laserHit.transform.position = barrel.transform.position + Quaternion.Euler(0, laserRotOffset, 0) * -transform.up * 100;
				line.material.mainTextureScale = new Vector2(100, 1);
			}
			if(hit.transform.tag == "Enemy") hit.transform.GetComponent<Enemy>().health -= dps * Time.deltaTime;
		}

		line.material.SetTextureOffset("_MainTex", new Vector2(-Time.time * 10, 0));
		laserHit.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
	}
	public void TurnOffLaser() {
		laserHit.SetActive(false);
		line.enabled = false;
	}
	void AmmoUI() {
		ammoText.text = Mathf.Round(bulletsLeft) + "/" + Mathf.Round(clipSize);
		if (bulletsLeft / clipSize < 0.3f) {
			reloadText.text = "RELOAD[R]";
			reloadText.color = new Color (Mathf.Sin (Time.time * 10), 0, 0);
		} else {
			reloadText.text = "Ammo:";
			reloadText.color = Color.black;
		}
		if(reloading){
			reloadText.text = "Reloading: " + Mathf.Round(reloadTimer * 10)/10f + ((Mathf.Round(reloadTimer * 10) % 10 == 0)? ".0": "");
			reloadText.color = Color.black;
		}
		if (clipSize == 0) {
			reloadText.text = "No Weapon";
			ammoText.text = "";
		}
	}
	public void UpdateCharacter() {
		transform.Find("pistol").gameObject.SetActive(false);
		transform.Find("rifle").gameObject.SetActive(false);
		transform.Find("shotgun").gameObject.SetActive(false);
		transform.Find("laser").gameObject.SetActive(false);
		if(clipSize > 0){
			switch(currentType) {
				case WeaponType.Auto:
					GetComponent<SpriteRenderer>().sprite = rifleHold;
					transform.Find("rifle").gameObject.SetActive(true);
					break;
				case WeaponType.Laser:
					GetComponent<SpriteRenderer>().sprite = rifleHold;
					transform.Find("laser").gameObject.SetActive(true);
					break;
				case WeaponType.Shotgun:
					GetComponent<SpriteRenderer>().sprite = rifleHold;
					transform.Find("shotgun").gameObject.SetActive(true);
					break;
				case WeaponType.Semi:
					GetComponent<SpriteRenderer>().sprite = pistolHold;
					transform.Find("pistol").gameObject.SetActive(true);
					break;
			}
		}
		if(clipSize == 0) 
			GetComponent<SpriteRenderer>().sprite = nothingHold;
	}

	public void DisableWeapon() {
		if (enabled)
			TurnOffLaser ();
		enabled = false;
	}

	public void EnableWeapon() {
		enabled = true;
	}
}
